package com.google.code.netbuilder.dal.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author VIME0812
 */
@XmlAccessorType(XmlAccessType.FIELD)
@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ManagedObject implements Serializable {

	private static final long serialVersionUID = -3195422993288652611L;
	@XmlTransient
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    private long id;
    @XmlAttribute(name = "name")
    @Column(name = "name")
    private String name;
    @XmlAttribute(name = "creation_date")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name = "creation_date")
    private Date creationDate;
    @XmlAttribute(name = "modification_date")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Column(name = "modification_date")
    private Date modificationDate;
    @XmlAttribute(name = "object_state")
    @Column(name = "object_state")
    private int objectState;
    @XmlTransient
    @Transient
    private String strType;

    public ManagedObject() {
        creationDate = new Date();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getObjectState() {
        return objectState;
    }

    public void setObjectState(int objectState) {
        this.objectState = objectState;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }

    public String getStrType() {
        return this.getClass().getSimpleName();
    }

    public void setStrType(String strType) {
        this.strType = strType;
    }

    @XmlID
    @XmlAttribute(name = "id")
    public String getXmlId() {
        return Long.toString(id);
    }

    public void setXmlId(final String xmlId) {
        id = Long.parseLong(xmlId);
    }

    @PreUpdate
    public void preUpdate() {
        modificationDate = new Date();
    }

    @Override
    public boolean equals(Object ob) {
        if (ob == null) {
            return false;
        }
        if (!(ob instanceof ManagedObject))
        {
            return false;
        }
        ManagedObject castedOb = (ManagedObject) ob;
        return id == castedOb.getId() && 
               (name != null ? name.equals(castedOb.getName()) : castedOb.getName() == null) &&
                objectState == castedOb.getObjectState();
    }

    @Override
    public String toString() {
        return "id=" + id + ", name=" + name + ", creationDate=" + creationDate + ", modificationDate=" + modificationDate + ", objectState=" + objectState;
    }
    
    public String getStringState() {
        
        String state = null;
        
        if(this.objectState == 0) {
            
            state = "Normal";
        }
        else if(this.objectState == 1) {
            state = "Reserved";
        }
        else if(this.objectState == 2) {
            state = "Damaged";
        }
        else if(this.objectState == 3) {
            state = "Reserved/Damaged";
        }
        else {
            state = "Undefined";
        }
        
        return state;
    }
}
